package com.zhuchaooasystem.presenter.home.fragment.oa.record;

import android.app.Activity;
import android.os.Environment;

import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.common.Key;

import java.io.File;
import java.util.ArrayList;

/**
 * @author yinsi
 */


public class LocalRecordPresenter extends BasePresenter<LocalRecordContract.View> implements LocalRecordContract.Presenter {

    public LocalRecordPresenter(Activity activity, LocalRecordContract.View view) {
        super(activity, view);
    }


    @Override
    public void getAudioPathFromSD() {
        ArrayList<String> picList = new ArrayList<>();
        // 得到sd卡内路径
        String imagePath = Environment.getExternalStorageDirectory().toString() + Key.PATH_LOCAL_RECORDS;
        // 得到该路径文件夹下所有的文件
        File mfile = new File(imagePath);
        if (null != mfile && mfile.isDirectory()) {
            File[] files = mfile.listFiles();
            // 将所有的文件存入ArrayList中,并过滤所有图片格式的文件
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (checkIsAudioFile(file.getPath())) {
                    picList.add(file.getPath());
                }
            }
        }
        mView.refreshData(picList);
    }

    /**
     * 检查扩展名，得到MP3格式的文件
     *
     * @param fName
     * @return
     */
    private static boolean checkIsAudioFile(String fName) {
        boolean isImageFile;
        // 获取扩展名
        String FileEnd = fName.substring(fName.lastIndexOf(".") + 1,
                fName.length()).toLowerCase();
        if (FileEnd.equals("mp3")) {
            isImageFile = true;
        } else {
            isImageFile = false;
        }
        return isImageFile;
    }

}
